<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <card-search @search="reload" />
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        :selection.sync="selection"
      >
        <!-- 表头工具栏 -->
        <template v-slot:toolbar>
          <el-button
            size="small"
            type="info"
            icon="el-icon-_save"
            class="ele-btn-icon"
            @click="onExport"
          >
           导出
          </el-button>
        </template>

        <template v-slot:account="{ row }">
          <div class="ele-cell-content">
            <div class="ele-cell-title">{{ row.figureAccount.name }} </div>
            <div class="ele-cell-title">{{ row.figureAccount.telphone }} </div>
          </div>
          
        </template>

        <template v-slot:is_status="{ row }">
          <el-tag v-if="row.is_status==1" type="info" effect="plain">待激活</el-tag>
          <el-tag v-if="row.is_status==2" type="success" effect="plain">已激活</el-tag>
        </template>


        <template v-slot:use_time="{ row }">

          <span v-if="row.use_time">{{ row.use_time }}</span>
          <span v-else>--</span>
          
        </template>
       
      </ele-pro-table>
    </el-card>
  </div>
</template>

<script>
  import CardSearch from './components/card-search';
  import { list } from '@/api/card/list';
  import { utils, writeFile } from 'xlsx';

  export default {
    name: 'CardAccount',
    components: {
      CardSearch,
    },
    data() {
      return {
        // 表格列配置
        columns: [
          {
            prop: 'account',
            label: '开发者',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
            slot: 'account',
          },
          {
            prop: 'card',
            label: '卡密',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
          },
          {
            prop: 'is_status',
            label: '激活状态',
            align: 'center',
            width: 150,
            resizable: false,
            slot: 'is_status',
            showOverflowTooltip: true
          },
          {
            prop: 'due_time',
            label: '到期时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
          },
          {
            prop: 'use_time',
            label: '激活时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
            slot: 'use_time'
          }
        ],
        // 表格选中数据
        selection: [],
        // 当前编辑数据
        current: null,
        // 是否显示编辑弹窗
        showEdit: false,
        // 是否显示分配回收员
        showTransfer: false,
        //弹框名称
        id:0,
        where:{
          is_status:'',
          nickname:'',
        },
        exportData:[],
      };
    },
    methods: {
      onExport() {

          console.log(this.where);

          list(this.where).then((data) => {
            this.exportData = data.list;

            let array = [
              ['开发者','卡密','到期时间', '状态', '激活时间']
            ];

            


            this.exportData.forEach((d, i) => {

                if(d.is_status == 1){
                  d.is_status = '未激活';
                }else if(d.is_status == 2){
                  d.is_status = '已激活';
                }

                if(!d.figureAccount || typeof d.figureAccount  === 'undefined' ||  typeof d.figureAccount.telphone === 'undefined' || d.figureAccount.telphone == null || d.figureAccount.telphone=== ''){
                  d.account = '无';
                }else{
                  d.account =  d.figureAccount.telphone ;
                }

                if( d.use_time  === 'undefined' || d.use_time== null || d.use_time === ''){
                  d.use_time = '无';
                }

                array.push([d.account, d.card, d.due_time, d.is_status, d.use_time]);
            });
            const sheet = utils.aoa_to_sheet(array);
            // 如果需要设置列宽可以这样写
            sheet["!cols"] = [
                { wch: 20 },
                { wch: 30 },
                { wch: 30 },
                { wch: 10 },
                { wch: 20 },
                { wch: 20 },
                { wch: 20 },
                { wch: 20 }
            ];
            writeFile(
                {
                    SheetNames: ['Sheet1'],
                    Sheets: {
                        Sheet1: sheet
                    }
                },
                '卡密列表.xlsx'
            );

          }).catch((e) => {
            this.$message.error(e.message);
          });


          
      },
      /* 表格数据源 */
      datasource({ page, limit, where, order }) {
        where = where ? where : {};
        this.where = where;
        return list({ ...where, ...order, page, limit });
      },
      /* 刷新表格 */
      reload(where) {
        where = where ? where : {};
        this.where = where;
        this.$refs.table.reload({ page: 1, where: where });
      },
    }
  };
</script>

<style lang="scss" scoped>

  .drag {
    background: #000 !important;
    background-image: linear-gradient(#333, #999) !important;
  }

  .ele-cell-content {
   
    overflow: hidden;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }

  .icon {
    width: 32px;
    height: 32px;
  }

  .singlePalaceGrid {
    flex: 50%;
    height: 93px;
    display: flex;
    align-items: center;
    box-sizing: border-box;
    padding-left: 28px;
  }
  .flex-column {
      flex-direction: column;
      justify-content: center;
  }
  .singlePalaceGrid .accumulateNum {
    margin-left: 8px;
    height: 62px;
    display: flex;
    flex-direction: column;
    justify-content: space-evenly;
  }
  .singlePalaceGrid .accumulateNum .Num {
    color: #1e6ceb;
    font-size: 20px;
  }

  .flex-align-center {
    display: flex;
    align-items: center;
  }
 
  .purple-bj{
    background-color: #EEE7FF;
  }
  .blue-bj{
    background-color: #E2F6F6;
  }
  .yellow-bj{
    background-color: #FFF0E1;
  }
  .green-bj{
    background-color: #E4FFD5;
  }
  .red-bj{
    background-color: #FFD8D8;
  }
  .piece-all{
    margin-top: 19px;
    border-radius: 10px;
    height: 94px;
    padding: 0 11px;
    .icon-right{
      margin-right: 12px;
    }
    .piece-text{
      div{
        color: #333;
        font-size: 30px;
        font-weight: bold;
      }
      span{
        color: #666;
        font-size: 16px;
      }
    }
  }
</style>